package com.faintlight.controller;

import com.faintlight.model.Gratitude;
import com.faintlight.service.GratitudeService;
import com.faintlight.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/gratitudes")
@Tag(name = "感恩练习", description = "感恩练习管理接口")
@RequiredArgsConstructor
public class GratitudeController {

    private static final Logger logger = LoggerFactory.getLogger(GratitudeController.class);

    private final GratitudeService gratitudeService;
    private final UserService userService;

    @PostMapping
    @Operation(summary = "记录新的感恩练习")
    public ResponseEntity<Gratitude> saveGratitude(@RequestBody Gratitude gratitude, @RequestHeader("X-User-Id") Long userId) {
        gratitude.setUser(userService.getUserById(userId));
        Gratitude saved = gratitudeService.saveGratitude(gratitude);
        return ResponseEntity.ok(saved);
    }

    @GetMapping("/user/{userId}")
    @Operation(summary = "获取用户的感恩记录")
    public ResponseEntity<List<Gratitude>> getUserGratitudes(@PathVariable Long userId) {
        List<Gratitude> gratitudes = gratitudeService.getUserGratitudes(userId);
        return ResponseEntity.ok(gratitudes);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除感恩记录")
    public ResponseEntity<Map<String, String>> deleteGratitude(@PathVariable Long id, @RequestHeader("X-User-Id") Long userId) {
        gratitudeService.deleteGratitude(id, userId);
        Map<String, String> response = new HashMap<>();
        response.put("message", "感恩删除成功");
        return ResponseEntity.ok(response);
    }
}